package com.binance.healthcenter.service;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.binance.master.enums.LanguageEnum;
import com.binance.master.enums.TerminalEnum;
import com.binance.master.error.BusinessException;
import com.binance.master.error.GeneralCode;
import com.binance.master.models.APIRequest;
import com.binance.master.models.APIRequestHeader;
import com.binance.master.models.APIResponse;
import com.binance.master.utils.JsonUtils;
import com.binance.messaging.api.msg.MsgApi;
import com.binance.messaging.api.msg.request.SendMsgRequest;
import com.binance.messaging.api.msg.response.MsgResponse;

import lombok.extern.log4j.Log4j2;

@Log4j2
@Service
public class MessageService {
    @Resource
    private MsgApi msgApi;

    public void sendMsg(String tplCode, String cellPhone, Map<String, Object> param, String userId) {
        SendMsgRequest requestMsg = new SendMsgRequest();
        requestMsg.setTplCode(tplCode);
        requestMsg.setRecipient(cellPhone);
        requestMsg.setData(param);
        requestMsg.setUserId(userId);
        requestMsg.setMobileCode("+86");
        requestMsg.setNeedIpCheck(false);
        requestMsg.setNeedSendTimesCheck(false);
        APIRequestHeader originRequest = new APIRequestHeader();
        originRequest.setLanguage(LanguageEnum.ZH_CN);
        originRequest.setTerminal(TerminalEnum.WEB);
        APIResponse<MsgResponse> resp = null;
        try {
            resp = msgApi.sendMsg(APIRequest.instance(originRequest, requestMsg));
            log.info("MessageService.sendMsg Resp:{}", JsonUtils.toJsonNotNullKey(resp));
            if (resp.getStatus() == APIResponse.Status.ERROR) {
                throw new BusinessException(GeneralCode.findByCode(resp.getCode()));
            }
        } catch (Exception e) {
            log.error("sendMsg failed,", e);
        }
    }
}
